package com.ms.game.slot.base;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.ms.game.common.container.BasicSetting;
import com.xpec.c4.db.model.basic.BasicGameModel;
import com.xpec.c4.game.utility.GameUtility;

public class DMBasicSeatInfoDatas<T> 
{
	
	private static final Log log = LogFactory.getLog(DMBasicSeatInfoDatas.class);
	
	private ConcurrentHashMap<String , T>  pool = new ConcurrentHashMap<String,T>();
	
	/**
	 * key: gainRateID
	 */
	private HashMap<Integer, ArrayList<T>> list = new HashMap<Integer, ArrayList<T>>();
	
	public DMBasicSeatInfoDatas()
	{
		
	}
	
	/**
	 * 新增
	 * @param e
	 * @return
	 */
	public boolean add(T e)
	{
		if(e instanceof DMBasicSeatInfo)
		{
			DMBasicSeatInfo tmp = (DMBasicSeatInfo)e;
			pool.put(tmp.seatid, e);
			
			int gainRateID = tmp.getGainRateID();
			ArrayList<T> gainList = list.get(gainRateID);
			if(gainList == null){
				gainList = new ArrayList<T>();
				list.put(gainRateID, gainList);
			}
			gainList.add(e);
			
			return true;
		}
		
		
		return printError();
	}
	
	/**
	 * 移除
	 * @param seatid
	 * @return
	 */
	public T remove(String seatid)
	{
		if(!pool.containsKey(seatid))
		{
			return null;
		}
		
		return pool.remove(seatid);
	}
	
	/**
	 * 設定
	 * @param seatid
	 * @param e
	 * @return
	 */
	public boolean put(String seatid , T e)
	{
		if(e instanceof DMBasicSeatInfo)
		{
			if(!pool.containsKey(seatid))
			{
				return false;
			}
			
			pool.put(seatid, e);
			return true;
		}
		
		return printError();
	}
	
	/**
	 * 讀取 by seatid
	 * @param seatid
	 * @return
	 */
	public T get(String seatid)
	{
		if(!pool.containsKey(seatid))
		{
			return null;
		}
		
		return pool.get(seatid);
	}
	
	public HashMap<Integer, ArrayList<T>> getAll() {
		return list;
	}
	
	/**
	 *  讀取 by machineid
	 * @param machineid
	 * @return
	 */
//	public T getByID(int machineid)
//	{
//		if(machineid >= list.size())
//		{
//			return null;
//		}
//		
//		return list.get(machineid-1);
//	}
	
	
	
	/**
	 * 讀取機台數目
	 * @return
	 */
	public int getSize(int gainRateID)
	{
		ArrayList<T> gainPool = list.get(gainRateID);
		if(gainPool != null){
			return gainPool.size();
		}
		else return 0;
	}
	
	
	/**
	 * 將seatinfo依據machineid做排序
	 */
	public void sortSeatInfoToList() {
		for (ArrayList<T> sortList : list.values()) {
			Collections.sort(sortList, new Comparator<T>() {
				@Override
				public int compare(T n1, T n2) {
					DMBasicSeatInfo s1 = (DMBasicSeatInfo) n1;
					DMBasicSeatInfo s2 = (DMBasicSeatInfo) n2;

					if (s1.machineid < s2.machineid) {
						return -1;
					} else {
						return 1;
					}

				}
			});
		}
	}
	
	/**
	 * 讀取分頁資料
	 * @param from
	 * @param to
	 * @return
	 */
	public ArrayList<T> getListByPage(int gainRateID, int from, int to) {
		int size = BasicSetting.getInstance().getPageSize();
		
		if(from < 1){
			from = 1;
		}
		
		if(to < 1){
			to = 1;
		}
		
		int fromidx = (from - 1) * size;
		int toidx = to * size;

		// System.out.println("from ="+from+" to="+to +
		// " size="+size+"["+gainRateID+"]");

		ArrayList<T> gainList = list.get(gainRateID);
		if (gainList == null) {
			return null;
		}

		if (fromidx >= gainList.size()) {
			return null;
		}

		if (toidx > gainList.size()) {
			toidx = gainList.size();
		}

		return new ArrayList<T>(gainList.subList(fromidx, toidx));
	}
	
	/**
	 * 讀取空機台座位
	 * @return
	 */
	public T randEmptySeat(int gainRateID)
	{
		ArrayList<T> gainList = list.get(gainRateID);
		if (gainList == null) {
			return null;
		}

		ArrayList<T> tmp = new ArrayList<T>();
		for (T e : gainList) {
			DMBasicSeatInfo seat = (DMBasicSeatInfo) e;
			if (seat.stat == BasicGameModel.EMPTY && !seat.isVerify) {
				tmp.add(e);
			}
		}
		
		if(tmp.size() == 0){
			return null;
		}

		int idx = GameUtility.randomInt(0, tmp.size() - 1);
		return tmp.get(idx);
	}
	
	/**
	 * 打印出不正確資料
	 * @return
	 */
	private boolean printError()
	{
		if(log.isErrorEnabled())
		{
			log.error("DMBasicSeatInfoData Child  Must be  Instanceof  [ com.ms.game.slot.base.DMBasicSeatInfo ] !! Now instanceof is False");
		}
		
		return false;
	}
	
	
}
